/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.device.intf;

import hxg.portmgr.core.device.Device;
import hxg.portmgr.core.find.Index;
import hxg.portmgr.core.find.MatchLevel;
import javax.xml.bind.annotation.*;

/**
 *
 * @author hxg
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {})
@XmlRootElement(name = "Intf")
public class Intf implements Comparable<Intf>, AbstractIntf {

    @XmlAttribute
    int sequence = 0;
    @XmlAttribute
    String index;
    @XmlAttribute
    String name;
    @XmlAttribute
    String desc;
    @XmlAttribute
    IntfType type = IntfType.Other;
    @XmlAttribute
    int typeIntValue;
    String mtu;
    String speed;
    @XmlAttribute
    String markName = "";
    @XmlTransient
    Device device = null;

    //<editor-fold defaultstate="collapsed" desc="getter and setter">
    @Override
    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name != null) {
            this.name = name;
        }
    }

    public IntfType getType() {
        if (type == null || type == IntfType.Other) {
            type = IntfType.getTypeByValue(typeIntValue);
        }
        return type;
    }

    public void setType(IntfType type) {
        this.type = type;
    }

    public int getTypeIntValue() {
        return typeIntValue;
    }

    public void setTypeIntValue(int typeIntValue) {
        this.typeIntValue = typeIntValue;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getMtu() {
        return mtu;
    }

    public void setMtu(String mtu) {
        this.mtu = mtu;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getMarkName() {
        return markName;
    }

    public void setMarkName(String markName) {
        this.markName = markName;
    }

    //public void setIntfMarkFromAttr(IntfMark IntfMarkFromAttr) {
    //    this.IntfMarkFromAttr = IntfMarkFromAttr;
    //}
    public int getSequence() {
        return sequence;
    }

    public void setSequence(int sequence) {
        this.sequence = sequence;
    }

    public Device getDevice() {
        return device;
    }

    public void setDevice(Device device) {
        this.device = device;
    }
    //</editor-fold>

    @Override
    @Index(name = "port", chsName = "端口", level = MatchLevel.ContainMatch)
    public String toString() {
//        if (device != null) {
//            switch (device.getModel().getIntfMarkFromAttr()) {
//                case All:
//                    return "[" + getSeq() + "]" + index + "-" + name + "-" + desc + "-" + markName;
//                case Self:
//                    return "[" + getSeq() + "]" + markName;
//                case Desc:
//                    return "[" + getSeq() + "]" + desc;
//                case Index:
//                    return "[" + getSeq() + "]" + index;
//                case Name:
//                    return "[" + getSeq() + "]" + name;
//            }
//        }
        return "[" + getSeq() + "]" + markName;
    }

    public int getSeq() {
        if (device != null) {
            return device.getOffsideSequence() + sequence;
        } else {
            return sequence;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + this.sequence;
        hash = 47 * hash + (this.index != null ? this.index.hashCode() : 0);
        hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 47 * hash + (this.desc != null ? this.desc.hashCode() : 0);
        hash = 47 * hash + this.type.toString().hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Intf)) {
            return false;
        }
        final Intf other = (Intf) obj;
        if (!this.getDevice().equals(other.getDevice())) {
            return false;
        }
        if (this.type != other.type) {
            return false;
        }
        if ((this.index == null) ? (other.index != null) : !this.index.equals(other.index)) {
            return false;
        }
        /*
         * if (this.sequence != other.sequence) { return false; } if
         * ((this.index == null) ? (other.index != null) :
         * !this.index.equals(other.index)) { return false; } if ((this.name ==
         * null) ? (other.name != null) : !this.name.equals(other.name)) {
         * return false; } if ((this.desc == null) ? (other.desc != null) :
         * !this.desc.equals(other.desc)) { return false; } if (this.type !=
         * other.type) { return false; }
         */
        return true;
    }

    @Override
    public int compareTo(Intf o) {
        if (this.getDevice() != o.getDevice()) {
            return this.getDevice().compareTo(o.getDevice());
        } else {
            return this.getIndex().compareTo(o.getIndex());
        }
    }
}
